package com.jneagle.xlstool.bzwgjmx.node.configuration;

import com.dwarfeng.dutil.basic.cna.model.DefaultReferenceModel;
import com.dwarfeng.dutil.basic.cna.model.ReferenceModel;
import com.dwarfeng.dutil.basic.gui.swing.MappingTableModel;
import com.dwarfeng.dutil.basic.gui.swing.MappingTableModel.MappingInfos;
import com.dwarfeng.dutil.basic.gui.swing.MappingTableModel.TableColumn;
import com.jneagle.xlstool.bzwgjmx.stack.bean.entity.*;
import com.jneagle.xlstool.bzwgjmx.stack.structure.ProgressStatus;
import com.jneagle.xlstool.bzwgjmx.view.handler.I18nHandler;
import com.jneagle.xlstool.bzwgjmx.view.handler.ModalHandler;
import com.jneagle.xlstool.bzwgjmx.view.structure.ModalItem;
import com.jneagle.xlstool.bzwgjmx.view.structure.OperationStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Configuration
public class ViewConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(ViewConfiguration.class);

    @Value("${i18n.local.ui}")
    private String localeString;

    @PostConstruct
    public void init() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            LOGGER.warn("系统不支持 NimbusLookAndFeel 主题，将使用默认主题", e);
        }
    }

    @Bean
    public Locale uiLocale() {
        String language = "";
        String country = "";
        String variant = "";
        String[] s = localeString.split("_", 3);
        if (s.length >= 1) {
            language = s[0];
        }
        if (s.length >= 2) {
            country = s[1];
        }
        if (s.length >= 3) {
            variant = s[2];
        }
        return new Locale(language, country, variant);
    }

    @Bean
    public ReferenceModel<String> notificationModel(I18nHandler i18nHandler) {
        return new DefaultReferenceModel<>(i18nHandler.getMessage("ui.notification.000"));
    }

    @Bean
    public ReferenceModel<OperationStep> operationStepModel() {
        return new DefaultReferenceModel<>(OperationStep.STRUCTURED_DATA_IMPORT);
    }

    @Bean
    public ReferenceModel<File> structuredFileModel(ModalHandler modalHandler) {
        boolean loadFlag = modalHandler.getParsedValidValue(
                ModalItem.MODAL_STRUCTURED_DATA_LAST_LOAD_FLAG, Boolean.class
        );
        File file = modalHandler.getParsedValidValue(ModalItem.MODAL_STRUCTURED_DATA_LAST_LOAD_FILE, File.class);
        if (!loadFlag) {
            file = null;
        }
        return new DefaultReferenceModel<>(file);
    }

    @Bean
    public ReferenceModel<String> structuredPasswordModel() {
        return new DefaultReferenceModel<>("");
    }

    @Bean
    public ReferenceModel<ProgressStatus> progressStatusModel() {
        return new DefaultReferenceModel<>(ProgressStatus.IDLE);
    }

    @Bean
    public MappingTableModel<StructuredData> structuredDataTableModel() {
        return new MappingTableModel<>(StructuredData.class, StructuredDataMappingInfos.class);
    }

    @Bean
    public MappingTableModel<StructuredErrorInfo> structuredErrorInfoTableModel() {
        return new MappingTableModel<>(StructuredErrorInfo.class, StructuredErrorInfoMappingInfos.class);
    }

    @Bean
    public ReferenceModel<List<File>> rawFileModel() {
        return new DefaultReferenceModel<>(new ArrayList<>());
    }

    @Bean
    public MappingTableModel<RawPart> rawPartTableModel() {
        return new MappingTableModel<>(RawPart.class, RawPartMappingInfos.class);
    }

    @Bean
    public MappingTableModel<RawPartData> rawPartDataTableModel() {
        return new MappingTableModel<>(RawPartData.class, RawPartDataMappingInfos.class);
    }

    @Bean
    public MappingTableModel<RawErrorInfo> rawErrorInfoTableModel() {
        return new MappingTableModel<>(RawErrorInfo.class, RawErrorInfoMappingInfos.class);
    }

    @Bean
    public ReferenceModel<Integer> rawDataSucceedCountModel() {
        return new DefaultReferenceModel<>(0);
    }

    @Bean
    public MappingTableModel<RawPart> rawPartConfirmTableModel() {
        return new MappingTableModel<>(RawPart.class, RawPartConfirmMappingInfos.class);
    }

    @Bean
    public MappingTableModel<ExportData> exportDataTableModel() {
        return new MappingTableModel<>(ExportData.class, ExportDataMappingInfos.class);
    }

    @Bean
    public MappingTableModel<ExportErrorInfo> exportErrorInfoTableModel() {
        return new MappingTableModel<>(ExportErrorInfo.class, ExportErrorInfoMappingInfos.class);
    }

    @Bean
    public ReferenceModel<File> exportFileModel(
            @Qualifier("structuredFileModel") ReferenceModel<File> structuredFileModel
    ) {
        return new DefaultReferenceModel<>(structuredFileModel.get());
    }

    @Bean
    public ReferenceModel<String> exportPasswordModel() {
        return new DefaultReferenceModel<>("");
    }

    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = int.class)
    @TableColumn(columnName = "ui.label.024", columnValueGetterName = "getProjectNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.026", columnValueGetterName = "getClassificationName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.027", columnValueGetterName = "getProductionName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.028", columnValueGetterName = "getSpecification", columnClass = String.class)
    @TableColumn(columnName = "ui.label.029", columnValueGetterName = "getDemand", columnClass = String.class)
    @TableColumn(columnName = "ui.label.030", columnValueGetterName = "getRemark", columnClass = String.class)
    private interface StructuredDataMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = Integer.class)
    @TableColumn(columnName = "ui.label.032", columnValueGetterName = "getErrorMessage", columnClass = String.class)
    private interface StructuredErrorInfoMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.040", columnValueGetterName = "getOriginFileName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.041", columnValueGetterName = "getParserName", columnClass = String.class)
    private interface RawPartMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = int.class)
    @TableColumn(columnName = "ui.label.024", columnValueGetterName = "getProjectNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.026", columnValueGetterName = "getClassificationName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.027", columnValueGetterName = "getProductionName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.028", columnValueGetterName = "getSpecification", columnClass = String.class)
    @TableColumn(columnName = "ui.label.029", columnValueGetterName = "getDemand", columnClass = String.class)
    @TableColumn(columnName = "ui.label.030", columnValueGetterName = "getRemark", columnClass = String.class)
    private interface RawPartDataMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.040", columnValueGetterName = "getOriginFileName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.041", columnValueGetterName = "getParserName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = Integer.class)
    @TableColumn(columnName = "ui.label.032", columnValueGetterName = "getErrorMessage", columnClass = String.class)
    private interface RawErrorInfoMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.040", columnValueGetterName = "getOriginFileName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.041", columnValueGetterName = "getParserName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.046", columnValueGetterName = "isConflictFlag", columnClass = boolean.class)
    @TableColumn(
            columnName = "ui.label.047", columnValueGetterName = "isAppendFlag", columnClass = boolean.class,
            editable = true, columnValueSetterName = "setAppendFlag",
            columnValueSettingPolicy = MappingTableModel.ColumnValueSettingPolicy.CALL_SETTER
    )
    private interface RawPartConfirmMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = int.class)
    @TableColumn(columnName = "ui.label.024", columnValueGetterName = "getProjectNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.025", columnValueGetterName = "getPartNo", columnClass = String.class)
    @TableColumn(columnName = "ui.label.026", columnValueGetterName = "getClassificationName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.027", columnValueGetterName = "getProductionName", columnClass = String.class)
    @TableColumn(columnName = "ui.label.028", columnValueGetterName = "getSpecification", columnClass = String.class)
    @TableColumn(columnName = "ui.label.029", columnValueGetterName = "getDemand", columnClass = String.class)
    @TableColumn(columnName = "ui.label.030", columnValueGetterName = "getRemark", columnClass = String.class)
    private interface ExportDataMappingInfos extends MappingInfos {
    }

    @TableColumn(columnName = "ui.label.023", columnValueGetterName = "getRowIndex", columnClass = Integer.class)
    @TableColumn(columnName = "ui.label.032", columnValueGetterName = "getErrorMessage", columnClass = String.class)
    private interface ExportErrorInfoMappingInfos extends MappingInfos {
    }
}
